package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import com.dongdongshop.service.ItemService;
import com.dongdongshop.util.CookieUtils;
import com.dongdongshop.vo.CartVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("cart")
public class CartController {

    @DubboReference
    private CartService cartService;

    //获取购物车
    @RequestMapping("cartList")
    @ResponseBody
    public List<CartVO> cartList(HttpServletRequest request,HttpServletResponse response){
        //判断有没有登录
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();

        //true就是是否开启utf-8编码格式的意思
        String cookieCart = CookieUtils.getCookieValue(request, "mycart", true);
        //避免空指针
        if (StringUtils.isBlank(cookieCart)){
            cookieCart = "[]";
        }
        //cookie中的购物车
        List<CartVO> cartListCookie = JSONObject.parseArray(cookieCart, CartVO.class);
        if(tbUser == null){//如果未登录，则查询找cookie

            return cartListCookie;
        }else {
            //如果已经登录，我们查询redis，并且将cookie中的数据与redis中的数据进行合并，合并完成删除cookid
            List<CartVO> cartListFromRedis = cartService.findCartListFromRedis(tbUser.getUsername());
            if (cartListCookie.size() > 0){
                //合并过后的集合
                List<CartVO> mergeCartList = cartService.mergeCartList(cartListFromRedis, cartListCookie);
                //还要删除cookie中的数据
                CookieUtils.deleteCookie(request,response,"mycart");
                return mergeCartList;
            }
            return cartListFromRedis;
        }
    }

    //添加购物车
    @RequestMapping("addCart")
    public String addCart(HttpServletRequest request,HttpServletResponse response,Long itemId,Integer num){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();

        //第一步，从cookie中取出购物车
        List<CartVO> cartList = cartList(request,response);
        //第二步，调购物车服务，组装我们的购物车
        cartList = cartService.addGoodsToCartList(cartList,itemId,num);

        if(tbUser == null){
            // 第三步，将组装的购物车放入到cookie中
            String cartListJson = JSONObject.toJSONString(cartList);
            CookieUtils.setCookie(request,response,"mycart",cartListJson,true);
            return "success-cart";
        }else {
            cartService.setCartListToRedis(tbUser.getUsername(),cartList);
            return "success-cart";
        }
    }

    @RequestMapping("addCartAjax")
    @ResponseBody
    public List<CartVO> addCartAjax(HttpServletRequest request,HttpServletResponse response,Long itemId,Integer num){
        TbUser tbUser = (TbUser) SecurityUtils.getSubject().getPrincipal();

        //第一步，从cookie中取出购物车
        List<CartVO> cartList = cartList(request,response);
        //第二步，调购物车服务，组装我们的购物车
        cartList = cartService.addGoodsToCartList(cartList,itemId,num);

        if(tbUser == null){
            // 第三步，将组装的购物车放入到cookie中
            String cartListJson = JSONObject.toJSONString(cartList);
            CookieUtils.setCookie(request,response,"mycart",cartListJson,true);
        }else {
            cartService.setCartListToRedis(tbUser.getUsername(),cartList);
        }
        return cartList;
    }
}
